package client;

import java.awt.*;

import javax.swing.*;
import javax.swing.border.*;

import net.miginfocom.swing.MigLayout;

import model.Message;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class ClientChatFrame extends JFrame implements KeyListener, ActionListener {

	private JPanel contentPane;
	private TextArea textArea;
	private TextField textField;
	private ClientController controller;
	/**
	 * Launch the application.
	 */
	public ClientChatFrame(ClientController controller) {
		this.controller=controller;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 429, 269);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[]", "[][][]"));
		
		Label label = new Label("chat with \"a person\"");
		contentPane.add(label, "cell 0 0");
		
		textArea = new TextArea();
		contentPane.add(textArea, "cell 0 1,grow");
		
		textField = new TextField();
		textField.setSize(new Dimension(0, 20));
		textField.setPreferredSize(new Dimension(0, 20));
		textField.addKeyListener(this);
		contentPane.add(textField, "cell 0 2,growx");
		setVisible(true);
	}

	public void showMessage(Message message) {
		textArea.append(message.toString()+"\n");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
	}

	@Override
	public void keyPressed(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if (keyCode == 10) {
			System.out.println(textField.getText());
			controller.sendMessage(textField.getText());
			textField.setText("");
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
